// 网上下载的mp3或者电影的文件名字都会有网址信息，文件名太长，请修改文件名，把原文件名的前一个字符和后两个字符取出来，再加上后缀名，组合成新的文件名，
// 如原文件名为如www.baidu-2012-5gyy-10.mp4，修改后文件名为w10.mp4。请编写代码实现批量修改

// int rename(const char * oldpath, const char * newpath);
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

int main()
{

    DIR *dir = NULL;
    struct dirent *dent = NULL;
    dir = opendir("/mnt/hgfs/homework/music");
    if (dir == NULL)
    {
        perror("opendir");
        exit(-1);
    }
    dent = readdir(dir);
    char *s = NULL;
    char num[50] = "/mnt/hgfs/homework/music/";
    char old[60] = {0};
    char new[60] = {0};

    while (dent != NULL)
    {
        memset(old, 0, 60);
        memset(new, 0, 60);
        strcpy(old, num);
        strcpy(new, num);
        if (dent->d_type == DT_REG)
        {
            strcat(old, dent->d_name);

            int i = strlen(new);
            new[i] = dent->d_name[0];

            s = strrchr(dent->d_name, '.');
            s = s - 2;
            strcat(new, s);

            printf("%s\n", old);
            printf("%s\n", new);

            rename(old, new);
        }
        dent = readdir(dir);
    }

    closedir(dir);
    return 0;
}
